import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/Layout'
import Home from '@/views/Home'
import store from '@/store'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/layout'
}, {
  path: '/login',
  component: () => import('@/views/Login'),
  // 路由独享守卫
  beforeEnter: (to, from, next) => {
    if (store.state.token) {
      next(false)
    } else {
      next()
    }
  }
}, {
  path: '/layout',
  component: Layout,
  redirect: '/layout/home',
  children: [
    {
      path: 'home',
      component: Home,
      meta: { // 路由对象里的信息(额外参数)
        isScroll: true, // 这个页面需要设置滚动
        top: 0 // 滚动的距离
      }
    }, {
      path: 'user',
      component: () => import('@/views/User')
    }
  ]
}, {
  path: '/search',
  component: () => import('@/views/Search')
}, {
  path: '/search/:keywords',
  component: () => import('@/views/Search/SearchResult.vue')
},
{
  // path: '/article',
  path: '/article/:aid',
  component: () => import('@/views/ArticleDetail')
}, {
  path: '/user/edit',
  component: () => import('@/views/User/UserEditor')
}, {
  path: '/chat',
  component: () => import('@/views/Chat')
}
]

const router = new VueRouter({
  routes
})
// 登录成功时, 不允许切换路径进入登录页面
// router.beforeEach((to, from, next) => {
//   // 有token, 不能去登录页
//   // 无token, 需要用户"权限"的才需要去登录页
//   if (store.state.token.length > 0 && to.path === '/login') {
//     next(false) // 阻止跳转在当前页
//   } else {
//     next()
//   }
// })

export default router
